package com.wgq.TestRongQi;

public class TestLinkList {
    private TestNode frist;
    private TestNode last;
    private  int  size;

    public int  size(){
        return size;
    }

    public void  add(Object obj){
        TestNode testNode =new TestNode();
        if(frist==null){
            testNode.setShang(null);
            testNode.setObj(obj);
            testNode.setNext(null);
            frist=testNode;
            last=testNode;
        }else {
           // last.setNext(obj);
            testNode.setShang(last);
            testNode.setObj(obj);
            testNode.setNext(null);
          //  System.out.println("1111\t"+last.getObj());
            last.setNext(testNode);
            last=testNode;
          // System.out.println("2222\t"+last.getObj());
        }
        size++;
    }

    //判断越界
    public void rangeCheck(int index){
        if(index<0||index>=size){
            try{
            throw  new Exception();
            }catch (Exception e){
                e.printStackTrace();
            }


        }
    }

    //获取当前索引下的值
    public Object get( int index){
        rangeCheck(index);
        TestNode temp=null;
        if(frist!=null){
            temp=frist;
        for(int i=0;i<index;i++){
            temp=temp.next;
        }
        }
        return temp.obj;
    }

    //根据索引获取Node
    public TestNode Node(int index){
        rangeCheck(index);
        TestNode temp=null;
        if(frist!=null){
            temp=frist;
            for(int i=0;i<index;i++){
                temp=temp.next;
            }
        }
        return temp;
    }

    //删除指定链表
    public void remove(int index){
      TestNode node =  Node(index);
        TestNode up = node.shang;
        TestNode down = node.next;
        up.next=down;
        up= down.shang;
        size--;
    }

    //根据指定索引添加到链表中
    public void add(int index, Object obj){
        TestNode node = Node(index);
        TestNode newNode = new TestNode();
        newNode.obj = obj;
        if(node.next!=null&&index!=0){  //在链表中间添加
         TestNode up = node.shang;
         up.next = newNode;
         newNode.shang = up;
         //   newNode.shang = node;
         newNode.next = node ;
         node.shang  = newNode;

         //   newNode =  node.shang.next;
         size++;
        }else if(index == 0) {  //在链表头添加
            newNode.shang = null;
            newNode.next = node;
            node.shang = newNode;
            frist=newNode;
            size++;

        }else {                 //在链表尾添加
           newNode.shang = node;
         //  newNode.obj = obj;
           newNode.next = null;
           node.next = newNode;
           last=newNode;
            size++;
        }

    }

    //public static
    public static void main(String[] args) {
        TestLinkList testLinkList = new TestLinkList();
        testLinkList.add("123456");
        testLinkList.add("654321");
        testLinkList.add("wang");
     //   HashMap
        System.out.println(testLinkList.size());
     //   testLinkList.remove(1);
        testLinkList.add(0,"qing");
        testLinkList.add(testLinkList.size-1,"good");
    for(int i=0;i<testLinkList.size;i++){
        System.out.println(testLinkList.get(i));
    }
    }
}
